package com.ahp.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.ahp.bean.EvaleObject;
import com.ahp.bean.Tast;
import com.ahp.service.EvaleObjectService;

public class EvaleObjectAction extends CommonActionSupport {
	private static final long serialVersionUID = 1L;
	private EvaleObject evaleObject;
	private EvaleObjectService evaleObjectService;
	public EvaleObject getEvaleObject() {
		return evaleObject;
	}
	public void setEvaleObject(EvaleObject evaleObject) {
		this.evaleObject = evaleObject;
	}
	public EvaleObjectService getEvaleObjectService() {
		return evaleObjectService;
	}
	public void setEvaleObjectService(EvaleObjectService evaleObjectService) {
		this.evaleObjectService = evaleObjectService;
	}
	
	/**
	 * 添加评估对象
	 * @return
	 * @date:Sep 6, 2009
	 * @author: 李晓娜
	 */
	public String addEvaleObject() {
		PrintWriter out = null;
		try {
			Integer id = evaleObjectService.addEvaleObject(evaleObject);
			StringBuffer sb = new StringBuffer();
			sb.append("{result:\"");
			if(id > 0){
				if(getSession().get("object")!=null){
					List<Integer> list = (List<Integer>)getSession().get("object");
					list.add(id);
				}else{
					List<Integer> list = new ArrayList<Integer>();
					list.add(id);
					getSession().put("object", list);
				}
				sb.append("right").append("\",id:\"").append(id).append("\"}");
			} else {
				sb.append("wrong").append("\"}");
			}
			
			//System.out.println(sb.toString());
			
			out = getResponse().getWriter();
			out.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
		return NONE;
	}
	
	/**
	 * @return
	 * @date:Sep 6, 2009
	 * @author: 李晓娜
	 */
	public String configEvaleObject() {
		List<EvaleObject> evaleObjectList = evaleObjectService.findEvaleObject(evaleObject.getTast().getTastId());
		getRequest().setAttribute("evaleObjs", evaleObjectList);
		return SUCCESS;
	}
	
	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String delEvaleObject() {
		boolean result = evaleObjectService.delEvaleObject(evaleObject);
		try {
			PrintWriter out = this.getResponse().getWriter();
			if(result){
				if(getSession().get("object")!=null){
					List<Integer> list = (List<Integer>)getSession().get("object");
					list.remove(evaleObject.getEvaleId());
				}
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return NONE;
	}
	
	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String listObject() {
		String taskId = getRequest().getParameter("evaleObject.tast.tastId");
		//List<EvaleObject> list = evaleObjectService.listObject(evaleObject.getTast().getTastId());
		List<EvaleObject> list = evaleObjectService.listObject(Integer.parseInt(taskId));
		
		Tast task = evaleObjectService.getTask(Integer.parseInt(taskId));
		getRequest().setAttribute("objectList", list);
		getRequest().setAttribute("task", task);
		return SUCCESS;
	}
}
